package org.klarton.gui;

import org.klarton.filter.Filter;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.MessageFormat;
import java.util.List;

/**
 * This panel creates for each single {@link org.klarton.filter.Filter} a {@link JProgressBar}.
 * The progress bars will be vertically orientated and labeled by the target frequency
 * and name of the associated filter.
 */
public class FrequencyAnalyzerPanel extends JPanel {

    public FrequencyAnalyzerPanel(List<Filter> filters) {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints(-1,1,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH, new Insets(0,0,0,0),0,0);

        for (final Filter filter : filters) {
            final JProgressBar bar = new JProgressBar(JProgressBar.VERTICAL);
            bar.setString(MessageFormat.format("{0,number,0.0} Hz - {1}", filter.getTargetFrequency(), filter.getName()));
            bar.setStringPainted(true);
            bar.setBackground(Color.WHITE);
            bar.setBorderPainted(false);
            bar.setForeground(Color.BLACK);

            filter.addPropertyChangeListener(Filter.VALUE, new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    int value = (int)filter.getValue();
                    bar.setValue(value);
                    int color = Math.max(255 - value, 0);
                    bar.setBackground(new Color(color, color, color));
                }
            });

            add(bar, gbc);
        }
    }
}
